package me.zhengjie.ship.shipenvbase.service.impl;

import me.zhengjie.ship.shipenvbase.domain.ShipEnvBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.ship.shipenvbase.repository.ShipEnvBaseRepository;
import me.zhengjie.ship.shipenvbase.service.ShipEnvBaseService;
import me.zhengjie.ship.shipenvbase.service.dto.ShipEnvBaseDto;
import me.zhengjie.ship.shipenvbase.service.dto.ShipEnvBaseQueryCriteria;
import me.zhengjie.ship.shipenvbase.service.mapper.ShipEnvBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author liwh
* @date 2020-04-22
*/
@Service
//@CacheConfig(cacheNames = "shipEnvBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShipEnvBaseServiceImpl implements ShipEnvBaseService {

    private final ShipEnvBaseRepository shipEnvBaseRepository;

    private final ShipEnvBaseMapper shipEnvBaseMapper;

    public ShipEnvBaseServiceImpl(ShipEnvBaseRepository shipEnvBaseRepository, ShipEnvBaseMapper shipEnvBaseMapper) {
        this.shipEnvBaseRepository = shipEnvBaseRepository;
        this.shipEnvBaseMapper = shipEnvBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ShipEnvBaseQueryCriteria criteria, Pageable pageable){
        Page<ShipEnvBase> page = shipEnvBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shipEnvBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ShipEnvBaseDto> queryAll(ShipEnvBaseQueryCriteria criteria){
        return shipEnvBaseMapper.toDto(shipEnvBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ShipEnvBaseDto findById(Integer id) {
        ShipEnvBase shipEnvBase = shipEnvBaseRepository.findById(id).orElseGet(ShipEnvBase::new);
        ValidationUtil.isNull(shipEnvBase.getId(),"ShipEnvBase","id",id);
        return shipEnvBaseMapper.toDto(shipEnvBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ShipEnvBaseDto create(ShipEnvBase resources) {
        return shipEnvBaseMapper.toDto(shipEnvBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ShipEnvBase resources) {
        ShipEnvBase shipEnvBase = shipEnvBaseRepository.findById(resources.getId()).orElseGet(ShipEnvBase::new);
        ValidationUtil.isNull( shipEnvBase.getId(),"ShipEnvBase","id",resources.getId());
        shipEnvBase.copy(resources);
        shipEnvBaseRepository.save(shipEnvBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shipEnvBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShipEnvBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShipEnvBaseDto shipEnvBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("舷号", shipEnvBase.getShipNo());
            map.put("硬件", shipEnvBase.getHardwareEnv());
            map.put("输入部件", shipEnvBase.getInputUnit());
            map.put("视频总线", shipEnvBase.getVideoBus());
            map.put("会商桌", shipEnvBase.getConferenceTable());
            map.put("软件架构", shipEnvBase.getSoftStruct());
            map.put("操作系统", shipEnvBase.getOperatingSystem());
            map.put("系统版本", shipEnvBase.getOperatingVersion());
            map.put("构建基本平台", shipEnvBase.getPlatBase());
            map.put("数据库（oracle/达梦等）", shipEnvBase.getDatabase());
            map.put("数据库版本（版本号）", shipEnvBase.getDatabaseVersion());
            map.put("数据库模式（单/双）", shipEnvBase.getDatabaseMode());
            map.put("QT版本", shipEnvBase.getQtVersion());
            map.put("开发工具", shipEnvBase.getDevelopmentTool());
            map.put("开始时间", shipEnvBase.getStartTime());
            map.put("结束时间", shipEnvBase.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}